﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudStorageLight.Core
{
    public class CustomAuthInfo
    {
        public string Key { get; set; }
        public string Type { get; set; }

        public string Name { get; set; }
        public string Path { get; set; }
        public string User { get; set; }
        public string Password { get; set; }
        public DateTime Expires { get; set; }
        public string AuthMode { get; set; }

        public bool HasCredentials
        {
            get
            {
                if (string.IsNullOrEmpty(User) || string.IsNullOrEmpty(Password)) return false;
                if (Expires < DateTime.UtcNow) return false;
                return true;
            }
        }

        public string GetAuthString()
        {
            var ticket = Expires.ToString("yyyyMMddHHmmss") + "\\" + User + "\\" + Password;
            return CryptUtil.EncryptStringAES(ticket, SystemSettings.Instance.GeneralEncryptKey);

        }

        public bool SetAuthInfo(string ticket)
        {
            var plain = CryptUtil.DecryptStringAES(ticket, SystemSettings.Instance.GeneralEncryptKey);
            var works = plain.Split(new char[] { '\\' }, 3);
            if (works.Length != 3) return false;

            Expires = DateTime.ParseExact(works[0], "yyyyMMddHHmmss", null);
            User = works[1];
            Password = works[2];
            return true;
        }
    }
}
